package org.example.demo.apiv2.sql;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import org.example.demo.apiv2.constant.ColumnDataType;
import org.example.demo.apiv2.entity.FieldEntity;

@Slf4j
public class ExpressionValueUtil {

    public static Expression getConstantValue(Object value, String dataType) {
        switch (dataType) {
            case ColumnDataType.STRING:
                return new StringValue(value.toString());
            case ColumnDataType.NUMBER:
                return new LongValue(value.toString());
            case ColumnDataType.DATE:
                return new DateValue(value.toString());
            case ColumnDataType.DATETIME:
                return new TimestampValue(value.toString());
            default:
                log.error("不支持的数据类型: {}", dataType);
                throw new IllegalArgumentException("不支持的数据类型: " + dataType);
        }
    }


    public static Expression getVariableValue(Object value) {
        return null;
    }


    public static Expression getFieldValue(Object value) {
        FieldEntity fieldEntity = BeanUtil.toBean(value, FieldEntity.class);
        return FieldParser.getColumn(fieldEntity);
    }

    public static Expression getSqlStringValue(Object value) {
        Statement statement = null;
        try {
            statement = CCJSqlParserUtil.parse(value.toString());
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }
        return (Select) statement;
    }

    public static Expression getExpressionStringValue(Object value) {
        try {
            return CCJSqlParserUtil.parseCondExpression(value.toString());
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }
    }
}
